<template>
  <!-- 主页组件 -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-title">
        <a href="#/welcomeTo" title="前往首页">学生防疫后台管理系统</a>
      </div>
      <div>
        <span class="welcome">欢迎您：{{this.$store.state.userInfo.username}}</span>
        <el-button type="primary" icon="el-icon-switch-button" plain @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 左侧菜单区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''"  v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.ps_icon"></i>
              <!-- 文本 -->
              <span class="menu-text">{{item.authName}}</span>
            </template>

            <!-- 一级菜单的二级菜单 -->
            <el-menu-item :index="subItem.path + ''" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.path + '')">
              <template slot="title">
                <!-- 图标 -->
                <i :class="subItem.ps_icon"></i>
                <!-- 文本 -->
                <span class="menu-text">{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据列表
      menuList: [],
      // 是否折叠左侧菜单栏
      isCollapse: false,
      // 被激活的二级菜单链接地址
      activePath: '',
      // vuex该登录用户的信息
      userInfo: []
    }
  },
  created () {
    // 生命周期函数，页面一加载就执行获取左侧菜单列表的方法
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  computed: {
    getUserInfo () {
      return this.$store.state.userInfo
    }
  },
  methods: {
    // 退出按钮事件
    async logout () {
      const confirmResult = await this.$confirm('是否退出该系统', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消退出！')
      }
      window.sessionStorage.clear()
      this.$router.push('/loginUser')
    },
    // 获取所有左侧菜单的方法
    async getMenuList () {
      const { data: res } = await this.$http.get('/my/getMenulist')
      // console.log(res)
      if (res.meta.status !== 0) return this.$message.error(res.meta.message)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    // 点击按钮切换菜单的折叠与展开方法
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存二级菜单链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }

}
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 16px;
}
.el-aside {
  background-color: #333744;
  color: #333;
  /* text-align: center; */
}
.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
}
.el-menu {
  border-right: 0;
}
.menu-text {
  margin: 0 0 0 15px;
}

.el-menu-item {
  background-color: #303133 !important;
}
.el-menu-item:hover {
  background-color: #606266 !important;
}
.welcome {
  margin-right: 40px;
}
.header-title > a {
  margin-left: 20px;
  letter-spacing: 3px;
  /* color: #409EFF; */
  font-size: 18px;
}
.header-title > a:hover {
  color: #409EFF;
}
</style>
